package com.qianfeng.tulingdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/12.
 */
public class MyAdapter extends BaseAdapter {
    //数据源
    //两个人对话类型不一样，
    //所以设定接口 用于兼容两种不用的实体类
    private List<Itext> mList;
    private Context mContext;

    public MyAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<>();
    }

    public void setList(List<Itext> list) {
        mList = list;
        notifyDataSetChanged();
    }

    public void addList(List<Itext> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    //用于在结尾增加一条
    public void addItem(Itext itext) {
        mList.add(itext);
        notifyDataSetChanged();
    }
    //获取view种类的总数
    //假设总数为n


    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getType();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    //item的类型 有两种 0.12.......n-1.n-2;
    @Override
    public long getItemId(int i) {
        return 0;
    }

    //这里要用到多布局， 根据类型判断是谁说的话

    @Override
    public View getView(int position, View convertview, ViewGroup parent) {
        int type = getItemViewType(position);
        ViewHolderMy holderMy = null;
        ViewHolderYour holderYour = null;
        if (convertview == null) {
            switch (type) {
                case 0://holderMy
                    holderMy = new ViewHolderMy();
                    convertview = LayoutInflater.from(mContext).inflate(R.layout.myview, parent, false);
                    holderMy.mTextView = (TextView) convertview.findViewById(R.id.mytext);
                    holderMy.mImageView = (ImageView) convertview.findViewById(R.id.myImage);
                    convertview.setTag(holderMy);
                    break;
                case 1://holderYour
                    holderYour = new ViewHolderYour();
                    convertview = LayoutInflater.from(mContext).inflate(R.layout.yourview, parent, false);
                    holderYour.mTextView = (TextView) convertview.findViewById(R.id.yourtext);
                    holderYour.mImageView = (ImageView) convertview.findViewById(R.id.yourImage);
                    convertview.setTag(holderYour);
                    break;
            }
        } else {
            switch (type) {
                case 0:
                    holderMy = (ViewHolderMy) convertview.getTag();
                    break;
                case 1:
                    holderYour = (ViewHolderYour) convertview.getTag();
                    break;
            }

        }
        //赋值
        switch (type) {
            case 0:
                MyText myText = (MyText) mList.get(position);
                holderMy.mTextView.setText(myText.getInfo());
                break;
            case 1:
                YourText yourText = (YourText) mList.get(position);
                holderYour.mTextView.setText(yourText.getInfo());
                break;

        }


        return convertview;
    }

    public static class ViewHolderMy {

        TextView mTextView;
        ImageView mImageView;
    }

    public static class ViewHolderYour {

        TextView mTextView;
        ImageView mImageView;
    }

}
